﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">
    <title>客户管理</title>
    <link href="/CSS/bootstrap3.3.5/css/bootstrap.css" rel="stylesheet" type="text/css" />
    <link href="/JqueryPlug/zTree_v3/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
    <link href="/CSS/font-awesome-4.3.0/css/font-awesome.css?v=4.3.0" rel="stylesheet"> 
    <link href="/CSS/V3/animate.css" rel="stylesheet">
    <link href="/CSS/V3/style.css?v=2.2.0" rel="stylesheet">
    <script src="/Scripts/jquery-1.11.2.min.js"></script>
    <script src="/CSS/bootstrap3.3.5/js/bootstrap.js"></script> 
    <script src="/JqueryPlug/laypage/laypage.js"></script>
    <script src="/Scripts/avalon.js"></script>
    <script src="/Scripts/jquery-migrate-1.1.0.min.js"></script>
    <script src="/Scripts/SZHLCommon.js?jsver=5"></script>
    <script src="/JqueryPlug/zTree_v3/js/jquery.ztree.core-3.5.js"></script>
    <script src="/JqueryPlug/zTree_v3/js/jquery.ztree.excheck-3.5.min.js"></script>
    <style>
        .icon-xitongshoucang {
            cursor: pointer;
            font-size: 18px;
        }

        .icon-yishoucang {
            cursor: pointer;
            color: gold;
        }
    </style>
    <script>
        var zNodes = null;
        var zTreeObj = null;
        var model = avalon.define({
            $id: "USUERKHRZLIST",
            menutype: "-1",//菜单类型(0:我的客户.-1:"当前部门客户")
            selmenu: function (val) {
                model.menutype = val;
                model.CommonData.clear();
                model.GetKHData()
                model.queryuser = "";
                if (model.menutype == "-1") {
                    $.getJSON('/API/SZHLAPI.ashx?Action=GETUSERTREE', { P1: "1", }, function (resultData) {//获取可以查看日报的人员列表
                        model.userlist = resultData.Result;
                    })
                }
            },//选中菜单事件
            queryuser: ComFunJS.getnowuser(),
            username: ComFunJS.getnowuser(),
            userlist: {},
            folderclick: function (item, dom) {
                $(".divuser").hide();
                $(dom).parent().find("div").eq(0).toggle();
            },
            fnqueryuser: function (item) {
                if (item) {
                    model.queryuser = item;
                    model.CommonData.clear();
                    model.GetKHData();
                }

            },
            KHType: "",
            KHContent: "",
            selKHType: "",
            selKHContent: "",
            CommonData: [],
            KHTypeData: [],
            TotalCount: "",
            UserCount: "",
            CRUserCount: "",
            ViewDT: function (id) {
                parent.ComFunJS.winviewform("/View/APPBaseView/APP_KHGL_LXJL.html?FormCode=KHGL&pageType=view&ID=" + id, "客户联系记录", "1000", "");
                //parent.ComFunJS.winviewform("/ViewV3/APPBaseView/APP_KHGL_LXJL.html?ID=" + id+"&r="+Math.random(), "客户信息", "1000", "");
            },
            KHGLADD: function () {
                parent.ComFunJS.winviewform("/View/APPBaseView/APP_ADD_WF.html?FormCode=KHGL", "添加数据", "1000", "");
            },
            GetKHType: function () {
                $.getJSON('/API/FTAPI.ashx?Action=GETCOLUMNLIST', { P1: 8 }, function (resultData) {
                    if (resultData.ErrorMsg == "") {
                        model.KHTypeData = resultData.Result;
                    }
                })
            },
            GetKHData: function () {
                $.getJSON('/API/FTAPI.ashx?Action=GETUSERKHGLDATA_PAGE', { P1: model.KHType, p: 1, P2: model.KHContent, b: model.menutype, QUSER: model.queryuser}, function (resultData) {
                    if (resultData.ErrorMsg == "") {
                        model.CommonData.clear();
                        model.CommonData = resultData.Result;
                        model.selKHType = model.KHType;
                        model.selKHContent = model.KHContent;
                        if (parseInt(resultData.Result1) > 0) {
                            $("#pageDiv").show();
                            laypage({
                                cont: 'pageDiv', //容器。值支持id名、原生dom对象，jquery对象。
                                pages: Math.ceil(parseInt(resultData.Result1) * 1.0 / 8), //通过后台拿到的总页数
                                curr: 1, //初始化当前页
                                jump: function (e) { //触发分页后的回调
                                    if (model.selKHType != model.KHType || model.selKHContent != model.KHContent) {
                                        e.curr = 1;
                                    }
                                    $.getJSON('/API/FTAPI.ashx?Action=GETUSERKHGLDATA_PAGE', { P1: model.KHType, p: e.curr, P2: model.KHContent, b: model.menutype, QUSER: model.queryuser }, function (resultData) {
                                        model.CommonData.clear();
                                        model.CommonData = resultData.Result;
                                        model.selKHType = model.KHType;
                                        model.selKHContent = model.KHContent;
                                    })
                                }
                            });
                        }
                        else {
                            $("#pageDiv").hide();
                        }
                    }
                })
            },
            ViewPrint: function (ID, event) {
                if (event) {
                    event.stopPropagation();
                }
                parent.ComFunJS.winold("/View/APPBaseView/KHGL_Print.html?ID=" + ID);
            },
            EditKHGL: function (Id) {

                parent.ComFunJS.winviewform("/View/APPBaseView/APP_ADD_WF.html?FormCode=KHGL&ID=" + Id, "添加数据", "1000", "");
            },
            DelKHGL: function (scel) {

                parent.ComFunJS.winconfirm("确认要删除么", function () {

                    $.post("/API/FTAPI.ashx", { Action: "DELKHGLBYID", P1: scel.ID }, function (result) {
                        var jsonresult = $.parseJSON(result)
                        if ($.trim(jsonresult.ErrorMsg) != "") {
                            ComFunJS.winwarning(jsonresult.ErrorMsg);
                        } else {
                            model.CommonData.remove(scel);
                            parent.ComFunJS.winsuccess('删除成功');
                        }
                    })
                }, function () { })
            },
            LB: function () {
                model.KHType = $(this).attr("vl");
                if ($(this).hasClass("btn-primary")) {
                    model.GetKHData()
                }
                else {
                    $(".leibie").removeClass("btn-primary");
                    $(".leibie").addClass("btn-white");
                    $(this).removeClass("btn-white");
                    $(this).addClass("btn-primary");
                    model.GetKHData()
                }
            },
            getUserTree: function () {
                $.getJSON('/API/SZHLAPI.ashx?Action=GETUSERTREE', { P1: "1", }, function (resultData) {//获取可以查看日报的人员列表
                    if (resultData.Result) {
                        model.userlist = resultData.Result;

                        var tree = [];
                        if (resultData.Result.SubBranch.length > 0) {
                            $(resultData.Result.SubBranch).each(function (index, item) {
                                var subt = [];
                                $(item.SubUsers).each(function (index2, item2) {
                                    subt.push({ "id": item2.UserName, "text": item2.UserRealName, "icon": "none" });
                                })
                                tree.push({ "id": item.BranchID, "text": item.BranchName, "children": subt })
                            })
                        } else {

                            var chindren = [];
                            $(resultData.Result.SubUsers).each(function (index, item) {
                                var subu = { "id": item.UserName, "text": item.UserRealName, "icon": "none" };
                                chindren.push(subu);
                            })
                            tree.push({ "id": resultData.Result.BranchID, "text": resultData.Result.BranchName, "state": { "opened": true }, "children": chindren });
                        }
                        $("#using_json").jstree({
                            "core": { "data": tree }
                        }).bind("select_node.jstree", function (e, data) {
                            var node = data.instance.get_node(data.selected);
                            if (data.instance.is_leaf(node)) {
                                model.fnqueryuser(node.id);
                            } else {
                                model.fnqueryuser(node.children);
                            }

                        });
                    }

                })
            },
            GuanZhuList: [],
            //关注用户列表
            GetGuanZhuList: function () {
                $.getJSON('/API/FTAPI.ashx?Action=GETGUANZHULIST', function (resultData) {
                    if (resultData.ErrorMsg == "") {
                        model.GuanZhuList = resultData.Result;
                    }
                })
            },
            SetCY: function (item,iscy) {//设置或取消常用客户
                if (iscy=="Y") {
                    item.UserBS = item.UserBS + "," + model.username;
                } else {
                    item.UserBS = item.UserBS.replace(model.username, "").replace(",,", ",");
                }
                $.getJSON('/API/FTAPI.ashx?Action=SETCY', { P1: item.ID, P2: item.UserBS }, function (resultData) {
                    if (resultData.ErrorMsg == "") {
                        model.GuanZhuList = resultData.Result;
                        parent.ComFunJS.winsuccess('操作成功');
                    }
                })
            },
            ViewPrint: function (ID, event) {
                if (event) {
                    event.stopPropagation();
                }
                parent.ComFunJS.winold("/View/APPBaseView/KHGL_Print.html?ID=" + ID);
            }, GuanZhuData: function () {
                var userNames = "";
                if (model.GuanZhuList) {
                    model.GuanZhuList.forEach(function (val) {
                        userNames += val.BGZUser + ",";
                    })
                }
                if (userNames) {
                    userNames = userNames.substring(0, userNames.length - 1);
                }
                model.fnqueryuser(userNames);
            }, SendMsg: function (item)
            {
                parent.ComFunJS.winviewform("/View/APPBaseView/APP_ADD_WF.html?FormCode=DXGL&phone=" + item.KHPhone, "添加数据", "1000", "");
            },
            IsShowTR: false,
            getMenu: function () {
                var setting = {
                    data: {
                        simpleData: {
                            enable: true,
                            idKey: "id",
                            pIdKey: "pId",
                            rootPId: 0
                        }
                    },
                    view: {
                        showLine: false
                    },
                    callback: {
                        onClick: model.zTreeOnClick
                    }
                };
                $.ajax({
                    type: "GET",
                    url: "/API/SZHLAPI.ashx",
                    dataType: "html",
                    data: { "Action": "GETALLBMUSERLIST" },
                    success: function (msg) {
                        if (JSON.parse(msg).Result) {
                            model.IsShowTR = true;
                        }
                        zTreeObj = $.fn.zTree.init($("#tree"), setting, eval(JSON.parse(msg).Result));
                    }
                });
            },
            zTreeOnClick: function (event, treeId, treeNode) {
                model.queryuser = "";
                model.CommonData.clear();
                if (treeNode.isUser) {
                    model.queryuser = treeNode.id;
                } else {
                    model.menutype = treeNode.id;
                    if (treeNode.children.length > 0 && treeNode.children[0].isUser) {

                        $(treeNode.children).each(function (i, user) {
                            model.queryuser += user.id + ",";
                        })
                        model.queryuser = model.queryuser.substring(0, model.queryuser.length - 1);
                    }
                }
                model.GetKHData();
            }
        });
        avalon.ready(function () {
            ComFunJS.initsetajax();
            model.GetKHType();
            model.GetKHData();
            model.getMenu();
            model.GetGuanZhuList();
        })

    </script>
</head>
<body class="gray-bg" ms-controller="USUERKHRZLIST">
    <div class="wrapper wrapper-content  animated fadeInRight">
        <div class="row">
            <div class="col-sm-3">
                <div class="ibox float-e-margins">
                    <div class="ibox-content" style="border: 0; border-right: 1px solid #e7eaec;">
                        <div class="file-manager">

                            <div class="hr-line-dashed"></div>
                            <button class="btn btn-primary btn-block" ms-click="KHGLADD()">添加客户</button>
                            <div class="hr-line-dashed"></div>
                            <h4>类型</h4>
                            <button class="btn btn-primary btn-sm leibie" ms-attr-vl="" ms-click="LB()" type="button">全部</button>
                            <button class="btn btn-white btn-sm leibie" ms-click="LB()" ms-repeat-item="KHTypeData" ms-attr-vl="item.TypeNO" style="margin-right:5px" type="button">{{item.TypeNO}}</button>

                            <div class="hr-line-dashed"></div>

                            <h4 class="tag-title"><a href="APP_KHGL.html">我的客户</a></h4>
                            <h4 class="tag-title" ms-if="GuanZhuList.size()>0" ms-click="GuanZhuData()"><a>我的关注</a></h4>
                            <ul class="tag-list" style="padding: 0">
                                <li ms-repeat-item="GuanZhuList" ms-click="fnqueryuser(item.BGZUser)">
                                    <button class="btn btn-outline " style="margin-right:5px;" ms-class-1="btn-default:item.BGZUser!=queryuser" ms-class-2="btn-warning:item.BGZUser== queryuser">{{ComFunJS.convertuser(item.BGZUser)}}</button>
                                </li>
                            </ul>
                            <div class="clearfix"></div>
                            <h4 class="tag-title" ms-if="model.userlist>0">他人客户</h4>
                            <ul id="tree" ms-if="model.IsShowTR" class="ztree"></ul>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-9">
                <div class="ibox">
                    <div class="box" style="margin-bottom:15px;">

                        <div class="pull-right mail-search">
                            <div class="input-group">
                                <input type="text" class="form-control input-sm" ms-duplex="KHContent" name="search" placeholder="搜索客户">
                                <div class="input-group-btn">
                                    <button type="submit" class="btn btn-sm btn-primary" ms-click="GetKHData()">
                                        搜索
                                    </button>
                                </div>
                            </div>
                        </div>
                        <h2>
                            客户管理
                        </h2>
                        <div class="mail-tools tooltip-demo m-t-md">

                            <div class="btn-group pull-right">
                                <!--<button class="btn btn-white btn-sm">
                                    <i class="fa fa-arrow-left"></i>
                                </button>
                                <button class="btn btn-white btn-sm">
                                    <i class="fa fa-arrow-right"></i>
                                </button>-->
                                <div id="pageDiv" class="text-center"></div>
                            </div>
                            <div class="clearfix"></div>
                        </div>

                    </div>

                </div>
                <div class="full-height-scroll">
                    <div class="table-responsive">
                        <table class="table  table-hover">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>客户名称</th>
                                    <th>类别</th>
                                    <th>客户单位</th>
                                    <th>联系电话</th>
                                    <th>创建人</th>

                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ms-repeat-el="CommonData">
                                    <td><i class="iconfont icon-xitongshoucang " ms-click="SetCY(el,'Y')" title="设为常用" ms-if="el.UserBS.indexOf(username)==-1"></i><i class="iconfont icon-yishoucang" ms-click="SetCY(el,'N')" ms-if="!(el.UserBS.indexOf(username)==-1)"></i></td>
                                    <td>
                                        <a data-toggle="tab" href="#contact-1" class="client-link">{{el.KHName}}</a>
                                    </td>
                                    <td>
                                        {{el.KHLeiBie}}
                                    </td>
                                    <td>{{el.KHDW}}</td>

                                    <td> {{el.KHPhone}}</td>
                                    <td> {{ComFunJS.convertuser(el.CRUser)}}</td>

                                    <td class="client-status">
                                        <a class=" btn btn-white btn-xs" ms-click="ViewDT(el.ID)"><i class="fa fa-search"></i> 查看 </a>
                                        <a class="btn btn-xs btn-default" ms-click="ViewPrint(el.ID, event)"><i class='fa fa-edit'></i> 打印快递单</a>
                                        <a class=" btn btn-primary btn-xs" ms-click="EditKHGL(el.ID)" ms-if="el.CRUser==username"><i class=" fa fa-pencil"></i> 编辑 </a>
                                        <a class="btn btn-danger btn-xs" ms-click="DelKHGL(el)" ms-if="el.CRUser==username"><i class="fa fa-trash-o"></i> 删除 </a>
                                        <a class="btn btn-info btn-xs" ms-click="SendMsg(el)" ms-if="el.CRUser==username"> 发送短信 </a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

